# -*- coding: utf-8 -*-
# @Date    : 2017-05-26 14:53:14
# @Author  : fancy (fancy@thecover.co)
import requests

url = 'http://upload.qiniu.com/'

headers = {
    'Host': 'upload.qiniu.com',
    'Content-Type': 'multipart/form-data;',
    'Content-Length': '500'
}

def upload_file():
    file = open('lufei.jpg', 'r')
    data = {'file': file}
    res = requests.post(url, data=data)
    print res.content


def upload_by_qiniu_sdk():
    from qiniu import Auth, put_file, etag, urlsafe_base64_encode
    import qiniu.config
    #需要填写你的 Access Key 和 Secret Key
    access_key = 'u73rnv0Zp--WDc_TN0McC_1L100s9KpIfhZHxKcI'
    secret_key = 'iydVsaNBOqsKfIzggZXXmuineAE2heWxaTUdK7Q7'
    #构建鉴权对象
    q = Auth(access_key, secret_key)
    #要上传的空间
    bucket_name = 'fancy'
    #上传到七牛后保存的文件名
    key = 'my-python-logo.png';
    #生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)
    #要上传文件的本地路径
    localfile = './lufei.jpg'
    ret, info = put_file(token, key, localfile)
    print(info)
    assert ret['key'] == key
    assert ret['hash'] == etag(localfile)


if __name__ == '__main__':
    upload_file()
